<template>
	<div>
    <div style="margin-bottom: 20px">
      <el-button type="primary" @click="templateAdd()"><i class="el-icon-plus"></i>模块</el-button>
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline" label-width="70px">
      <el-form-item label="所属系统">
        <el-select v-model="formInline.system" placeholder="所属系统" clearable style="width:140px">
          <el-option v-for="(item, index) in systemGroup" :value="item.value" :label="item.label" :key="index" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="所属广告">
        <el-select v-model="formInline.ad" placeholder="所属广告" clearable style="width:140px">
          <el-option v-if="formInline.system !== '2'" label="BANNER" value="1"></el-option>
          <el-option v-if="formInline.system !== '2'" label="开机广告" value="2"></el-option>
          <el-option v-if="formInline.system !== '2'" label="页面广告" value="3"></el-option>
          <el-option v-if="formInline.system !== '1'" label="款台广告" value="4"></el-option>
          <!-- <el-option v-for="(item, index) in equipmentGroup" :value="item.value" :label="item.label" :key="index" ></el-option> -->
        </el-select>
      </el-form-item>
      <el-form-item label="模块名称">
        <el-input v-model="formInline.name" placeholder="动作名称" style="width:140px"></el-input>
      </el-form-item>
      <el-form-item label="标识ID" label-width="60px">
        <el-input v-model="formInline.markid" placeholder="标示ID" style="width:140px"></el-input>
      </el-form-item>
      <el-form-item label="状态" label-width="50px">
        <el-select v-model="formInline.state" placeholder="状态" clearable style="width:140px">
          <el-option label="已生效" value="1"></el-option>
          <el-option label="已作废" value="2"></el-option>
        </el-select>
      </el-form-item>
			<el-form-item  style="float:right">
        <el-button type="primary"  @click="searchList">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="isLoading" :data="tableData" style="width: 100%" border>
      <el-table-column prop="mid" label="ID" align="center" width="100"></el-table-column>
      <el-table-column label="所属系统" align="center">
        <template scope="scope">
          <span v-if="scope.row.system === 1">订货系统</span>
          <span v-if="scope.row.system === 2">款台系统</span>
          <span v-if="scope.row.system === 3">业代行程</span>
          <span v-if="scope.row.system === 4">惠民物流</span>
        </template>
      </el-table-column>
      <el-table-column label="所属广告" align="center">
        <template scope="scope">
          <span v-if="scope.row.ad === 1">BANNER</span>
          <span v-if="scope.row.ad === 2">开机广告</span>
          <span v-if="scope.row.ad === 3">页面广告</span>
          <span v-if="scope.row.ad === 4">款台广告</span>
        </template>
      </el-table-column>
      <el-table-column prop="equipmentName" label="所属设备" align="center"></el-table-column>
      <el-table-column prop="markId" label="动作标识ID" align="center" width="100"></el-table-column>
      <el-table-column prop="name" label="模块名称" align="center"></el-table-column>
      <el-table-column label="模块状态" align="center" width="100">
        <template scope="scope">
          <span v-if="scope.row.state === 1">已生效</span>
          <span v-if="scope.row.state === 2">已作废</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template scope="scope">
          <el-button  @click="handle(scope.$index, scope.row, $event)" size="small">详情</el-button>
          <el-button v-if="scope.row.state === 1" @click="handle(scope.$index, scope.row, $event)" size="small">编辑</el-button>
          <el-button v-if="scope.row.state === 1" @click="handle(scope.$index, scope.row, $event)" size="small">作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <el-pagination
      @current-change="handleCurrentChange"
      :page-size="Number(formInline.pageSize)"
      layout="total, prev, pager, next"
      :total="Number(formInline.total)"
      style="padding-top:10px;text-align: right;">
    </el-pagination>
    <el-dialog title="跳转动作详情" :visible.sync="coupontDeteil">
      <div class="detail-info" style="margin-top: 43px">
        <div class="form-item">
          <el-form label-width="180px">
            <el-form-item label="所属系统">
              <span>{{this.detailData.systemName}}</span>
            </el-form-item>
            <el-form-item label="所属广告">
              <span>{{this.detailData.adName}}</span>
            </el-form-item>
            <el-form-item label="所属设备">
              <span>{{this.detailData.equipmentName}}</span>
            </el-form-item>
            <el-form-item label="动作标识">
              <span>{{this.detailData.markId}}</span>
            </el-form-item>
            <el-form-item label="动作名称">
              <span>{{this.detailData.name}}</span>
            </el-form-item>
            <el-form-item label="动作说明">
              <span>{{this.detailData.memo}}</span>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </el-dialog>
	</div>
</template>
<script>
  import { gggl } from '../../../config/proxy.js'
  // import { mapState } from 'vuex'
  import mixins from '../../../components/mixins.vue'
//  import { apiURL } from '../../config/settings.js'
  export default {
    mixins: [mixins],
    data () {
      return {
        formInline: {
          ad: '', // 所属广告
          system: '', // 所属系统
          markid: '', // 活动标示
          name: '', // 名称
          state: '', // 状态
          pageSize: 10,
          total: '',
          page: 1
        },
        systemGroup: [
          {label: '订货系统', value: '1'},
          {label: '款台系统', value: '2'}
          // {label: '业代行程', value: '3'},
          // {label: '惠民物流', value: '4'}
        ],
        equipmentGroup: [
          {label: 'pc', value: '1'},
          {label: '惠付通', value: '2'},
          {label: '惠配通', value: '3'}
        ],
        coupontDeteil: false,
        branchList: [],
        couponInfoList: [],
        checkInfoList: [],
        departmentList: [],
        tableData: [],
        isLoading: false,
        detailData: {},
        detailDialog: false
      }
    },
    methods: {
      templateAdd () { // 新增兑换码
        this.openNewWindow('mkgl_add', '新增模块管理', 'mkgl_add')
      },
      searchList () { // 查询
        this.isLoading = true
        var _data = this.formInline
        console.log(_data)
        gggl.mkgl.list(_data, (res) => { // 查询
          console.log(res)
          if (res.message === 'ok') {
            this.isLoading = false
            this.tableData = res.content.content
            this.formInline.total = res.content.count
            this.formInline.page = res.content.page
          } else {
            this.message('查询失败', 'error')
          }
        })
      },
      handleCurrentChange (val) {
        this.formInline.page = val
        this.tableData = []
        this.isLoading = true
        this.searchList()
      },
      handle (index, row, event) { // 操作
        const handleName = event.target.innerText
        switch (handleName) {
          case '详情':
            this.coupontDeteil = true
            console.log(row.mid)
            gggl.mkgl.detail({mid: row.mid}, (res) => {
              console.log(res, '详情')
              if (res.message === 'ok') {
                this.detailData = res.content
                this.detailDialog = true
              } else {
                this.message('查看详情', 'error')
              }
            })
            // this.openNewWindow('zcfqDetail', '注册返券详情', 'zcfqDetail', row.id)
            break
          case '编辑':
            this.openNewWindow('mkgl_bj', '编辑模块管理', 'mkgl_add', row)
            break
          // case '删除':
          //   this.buttonPublicMethods('删除', 'delete', row)
          //   break
          // case '撤回':
          //   this.buttonPublicMethods('撤回', 'invoke', row)
          //   break
          case '作废':
            this.buttonPublicMethods('作废', 'delete', row)
            break
        }
      },
      buttonPublicMethods (text, fnNane, row) { // 删除 撤回 作废 公共方法
        this.$confirm('您确定要' + text + '吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var _data = {}
          _data.mid = row.mid
          console.log(_data)
          console.log(fnNane)
          gggl.mkgl[fnNane](_data, (res) => {
            console.log(res)
            if (res.message === 'ok') {
              this.searchList()
              this.message(text + '成功', 'success')
            } else {
              this.message(text + '失败', 'error')
            }
          })
        }).catch(() => {
        })
      },
      // openNewWindow (menuId, menuName, perController, uid) { // 新增tab方法
      //   var _data = {
      //     menu_id: menuId,
      //     menu_name: menuName,
      //     per_controller: perController
      //   }
      //   if (uid) {
      //     _data.uid = uid
      //   }
      //   this.$store.dispatch('addTab', _data)
      // },
      timeDeltSecond (d) { // 时间去秒
        if (d) {
          return d.split(' ')[0]
        }
      },
      daterangeFormat (t) { // 时间段格式化
        console.log(t.split(' - '))
        var tGroup = t.split(' - ')
        this.formInline.stime = tGroup[0]
        this.formInline.etime = tGroup[1]
      },
      message (text, type) { // 消息提示
        this.$message({
          showClose: true,
          message: text,
          type: type
        })
      }
    },
    computed: {
      update_state: function () {
        // console.log(state.updateState.dhmState)
        return this.$store.state.updateState.mkglState
      },
      userName: function () {
        if (!this.$store.state.user.info.user_name) {
          this.$store.dispatch('SET_TOKEN')
        }
        return this.$store.state.user.info.user_name
      },
      userId: function () {
        return this.$store.state.user.info.roleid
      },
      branchName: function () {
        return this.$store.state.user.info.branchId === 1 ? '总公司' : this.$store.state.user.info.branchName
      },
      branchId: function () {
        return this.$store.state.user.info.branchId
      },
      deptName: function () {
        return this.$store.state.user.info.parOrgName
      },
      deptId: function () {
        return this.$store.state.user.info.or_org_id
      }
    },
    watch: {
      update_state (curVal, oldVal) {  // 监听 更新状态 查询list
        this.searchList()
      }
    },
    created () {
    }
  }
</script>
<style>
.line{
	margin-left:20px;
	text-align:center
}
.el-button+.el-button{
  margin-left: 0
}
.el-table .cell, .el-table th>div{
  padding-left:10px;
  padding-right:10px;
}
.el-dialog--small{
  width:60%
}
</style>
